package private

import (
	"dc-go/global"
	"dc-go/model"
	"dc-go/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ApiOrgRole struct{}

func (a *ApiOrgRole) GetOrgRoleList(c *gin.Context) {
	var orgCode model.RequestOrgCode
	if err := model.RequestShouldBindJSON(c, &orgCode); err != nil {
		return
	}
	if roleMenuList, err := servicePrivate.ServiceOrgRole.GetOrgRoleList(&orgCode); err != nil {
		global.DcLogger.Error("获取组织角色列表失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("获取组织角色列表失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessData(gin.H{"records": roleMenuList}, c)
	}
}

func (a *ApiOrgRole) EditOrgRole(c *gin.Context) {
	var orgRole model.RequestOrgRoleEdit
	if err := model.RequestShouldBindJSON(c, &orgRole); err != nil {
		return
	}
	empeeAcct := utils.GetEmpeeAcct(c)
	if err := servicePrivate.ServiceOrgRole.EditOrgRole(&orgRole, empeeAcct); err != nil {
		global.DcLogger.Error("编辑组织角色失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("编辑组织角色失败, "+err.Error(), c)
	} else {
		global.DcLogger.Warn(utils.GetEmpeeAcct(c) + "编辑组织角色成功! ")
		model.ResponseSuccessMessage("编辑组织角色成功! ", c)
	}
}

func (a *ApiOrgRole) QueryUserByOrgRole(c *gin.Context) {
	var orgRoleCode model.RequestOrgRoleCode
	if err := model.RequestShouldBindJSON(c, &orgRoleCode); err != nil {
		return
	}
	if userList, err := servicePrivate.ServiceOrgRole.QueryUserByOrgRole(&orgRoleCode); err != nil {
		global.DcLogger.Error("查找组织角色用户失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("查找组织角色用户失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessData(gin.H{"records": userList}, c)
	}
}

func (a *ApiOrgRole) RemoveUserOrgRole(c *gin.Context) {
	var toDeleteUserOrgRole model.RequestUserOrgRole
	if err := model.RequestShouldBindJSON(c, &toDeleteUserOrgRole); err != nil {
		return
	}
	if toDeleteUserOrgRole.EmpeeAcct == "admin" {
		model.ResponseErrorMessage("抱歉, 你不能修改超级管理员! ", c)
		return
	}
	if err := servicePrivate.ServiceOrgRole.RemoveUserOrgRole(&toDeleteUserOrgRole); err != nil {
		global.DcLogger.Error("移除组织角色用户失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("移除组织角色用户失败, "+err.Error(), c)
	} else {
		global.DcLogger.Warn(utils.GetEmpeeAcct(c) + "移除组织角色用户成功! ")
		model.ResponseSuccessMessage("移除组织角色用户成功! ", c)
	}
}

func (a *ApiOrgRole) AddUserOrgRole(c *gin.Context) {
	var toAddUserOrgRole model.RequestUserOrgRoleAdd
	if err := model.RequestShouldBindJSON(c, &toAddUserOrgRole); err != nil {
		return
	}
	if err := servicePrivate.ServiceOrgRole.AddUserOrgRole(&toAddUserOrgRole); err != nil {
		global.DcLogger.Error("添加组织角色用户失败! ", zap.Any("err", err))
		model.ResponseErrorMessage("添加组织角色用户失败, "+err.Error(), c)
	} else {
		model.ResponseSuccessMessage("添加组织角色用户成功! ", c)
	}
}
